package com.suotuo.module.sys.service.impl;


import com.suotuo.module.sys.dao.GameDAO;
import com.suotuo.module.sys.dao.HistoryDAO;
import com.suotuo.module.sys.dao.PlayCheckDAO;
import com.suotuo.module.sys.dto.GameDTO;
import com.suotuo.module.sys.entity.Games;
import com.suotuo.module.sys.service.GameService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author liulubingjava@163.com
 * @data 2019/1/18 16:29
 */
@Service
public class GameServiceImpl implements GameService {
    @Autowired
    private GameDAO gameDAO;
    @Autowired
    private HistoryDAO historyDAO;
    @Autowired
    private PlayCheckDAO playCheckDAO;
    @Override
    public Games getGame(String gameId) {
        return gameDAO.findById(gameId).get();
    }

    @Override
    public Page<Games> findAll(int page,int size,String sort,String propertie) {

        //PageRequest的对象构造函数有多个，page是页数，初始值是0，size是查询结果的条数，后两个参数参考Sort对象的构造方法
        if (sort.isEmpty()||sort.equals("asc")){
            PageRequest pageRequest = PageRequest.of(page,size,Sort.Direction.ASC,propertie);
            return gameDAO.findAll(pageRequest);
        }
        PageRequest pageRequest = PageRequest.of(page,size,Sort.Direction.DESC,propertie);
        return gameDAO.findAll(pageRequest);
    }
    @Override
    public void save(GameDTO gameDTO) {
        Games games = new Games();
        games.setGameId(getMaxId());
        games.setGameName(gameDTO.getGameName());
        games.setGameIcon(gameDTO.getGameIcon());
        games.setGamePic(gameDTO.getGamePic());
        games.setGameStatus(gameDTO.getGameStatus());
        games.setGameProfile(gameDTO.getGameProfile());
        games.setCreatTime(new Timestamp(System.currentTimeMillis()));
        games.setHotStatus(gameDTO.getHotStatus());
        games.setRecommendStatus(gameDTO.getRecommendStatus());
         gameDAO.save(games);
    }

    @Override
    public void update(GameDTO gameDTO,String gameId) {
        Games games  = gameDAO.findById(gameId).get();
        BeanUtils.copyProperties(gameDTO,games);
    }

    @Override
    public void deleteOne(String gameId) {
       gameDAO.deleteById(gameId);
       historyDAO.deleteByGameId(gameId);
       playCheckDAO.deleteByGameId(gameId);
    }

    @Override
    public void deleteMore(List<String> gameIdList) {
       for (String gameId : gameIdList){
           gameDAO.deleteById(gameId);
           historyDAO.deleteByGameId(gameId);
           playCheckDAO.deleteByGameId(gameId);
       }
    }

    @Override
    public Page<Games> findByCategoryId(String categoryId,int page,int size) {
        PageRequest pageRequest = PageRequest.of(page,size, Sort.Direction.DESC,"create_time");
        return  gameDAO.findByCategoryId(categoryId,pageRequest);
    }

    @Override
    public String getMaxId() {
        String s = gameDAO.getMaxId();
        Integer result = Integer.parseInt(s) + 1;
        return result.toString();
    }
}
